function paginationProvider() {

    this.data = [];
    this.max = 5;
    this.offset = 0;
    this.currentPage = 1;
    this.maxPage = 0;
    this.count = 0;
    this.controller = "";
    this.action = "";
    this.parameters = "";
    this.sort = "";
    this.http = null;

    this.init = function($http, controller, action, parameters, sort) {
	this.controller = controller;
	this.action = action;
	this.parameters = parameters;
	this.sort = sort;
	this.http = $http;
	this.loadData();
	return this.data;
    };

    this.loadData = function() {
	this.http.jsonp(
		'http://localhost:8090/CarManagement/' + this.controller + '/'
			+ this.action + '?' + this.parameters + '&max='
			+ this.max + '&offset=' + this.offset + '&sort='
			+ this.sort + '&callback=JSON_CALLBACK').success(
		function(data) {
		    this.data = data;
		}).error(function(data, status, headers, config) {
	    this.data = [ "Echec !", status, headers, config ];
	});
	this.http.jsonp(
		'http://localhost:8090/CarManagement/' + this.controller
			+ '/count?' + this.parameters
			+ '&callback=JSON_CALLBACK').success(function(data) {
	    this.count = data.count;
	    this.maxPage = this.getMaxPage();
	});
    };

    this.getNextPage = function() {
	var nextPage = this.currentPage + 1;
	if (nextPage <= maxPage) {
	    var newOffset = this.offset + this.max;
	    this.offset = newOffset;
	    this.currentPage++;
	    this.http.jsonp(
		    'http://localhost:8090/CarManagement/' + this.controller
			    + '/' + this.action + '?' + this.parameters
			    + '&max=' + this.max + '&offset=' + this.offset
			    + '&callback=JSON_CALLBACK').success(
		    function(data) {
			this.data = data;
		    }).error(function(data, status, headers, config) {
		this.data = [ "Echec !", status, headers, config ];
	    });
	}
    };

    this.getPage = function(pageNumber) {
	this.offset = this.max * (pageNumber - 1);
	this.currentPage = pageNumber;
	this.http.jsonp(
		'http://localhost:8090/CarManagement/' + this.controller + '/'
			+ this.action + '?' + this.parameters + '&max='
			+ this.max + '&offset=' + this.offset
			+ '&callback=JSON_CALLBACK').success(function(data) {
	    this.data = data;
	}).error(function(data, status, headers, config) {
	    this.data = [ "Echec !", status, headers, config ];
	});
    };

    this.getPreviousPage = function() {
	var newOffset = this.offset - this.max;
	if (newOffset >= 0) {
	    this.offset = newOffset;
	    this.currentPage--;
	    this.http.jsonp(
		    'http://localhost:8090/CarManagement/' + this.controller
			    + '/' + this.action + '?' + this.parameters
			    + '&max=' + this.max + '&offset=' + this.offset
			    + '&callback=JSON_CALLBACK').success(
		    function(data) {
			this.data = data;
		    }).error(function(data, status, headers, config) {
		this.data = [ "Echec !", status, headers, config ];
	    });
	}
    };

    this.getMaxPage = function() {
	var div = this.count / this.max;
	var rem = this.count % this.max;
	var max = Math.floor(div) + rem;
	return max;
    };

    this.getPageNumbers = function() {
	var result = [];
	for ( var i = 1; i <= maxPage; i++) {
	    result.push(i);
	}
	return result;
    };
};
